package com.exam.interceptor;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.exam.filter.AuthorityFilter;
import com.exam.util.Constants;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 登录检查拦截器
 * 
 * @author Administrator
 * 
 */
public class AuthorityInterceptor extends AbstractInterceptor {
	private static Log logger = LogFactory.getLog(AuthorityInterceptor.class);

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext ctx = invocation.getInvocationContext();
		Map session = ctx.getSession();
		HttpServletRequest request = (HttpServletRequest) ctx
				.get(ServletActionContext.HTTP_REQUEST);
		String path = request.getRequestURI();
		logger.info("AuthorityInterceptor:" + path);
		if (session.get(Constants.CURRENT_USER) != null
				|| path.contains("/exam/login")) {
			logger.info("pass!");
			return invocation.invoke();
		}
		// 没有登录，返回登录
		return Action.LOGIN;
	}

}
